package com.shiguiwu.springmybatis.algorithm.stack;

/**
 * @description: 数组模拟队列
 * @author: stone
 * @date: Created by 2021/3/10 20:43
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springmybatis.algorithm.stack
 */
public class ArrayStack {
    //栈的大小
    private int maxSize;

    //栈的大小指针
    private int top;

    //存放数据
    private int[] stack;

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        this.stack = new int[maxSize];
        this.top = -1;
    }


    /**
     * 是否为满
     * @return
     */
    public boolean isFull() {
        return this.top == this.maxSize - 1;
    }


    /**
     * 是否空
     * @return
     */
    public boolean isEmpty() {
        return this.top == -1;
    }

    /**
     * 入栈
     * @param data
     */
    public void push(int data) {
        if (this.isFull()) {
            System.out.println("栈空间已满~~~");
            return;
        }
        top++;
        stack[top] = data;
    }

    /**
     * 出栈
     * @return
     */
    public int pop() {
        if (isEmpty()) {
            System.out.println("栈空,不能出队~~~");
            throw new RuntimeException("栈空,不能出队~~~");
        }

        int data = stack[top];
        top--;
        return data;

    }

    /**
     * 遍历
     */
    public void print() {
        if (isEmpty()) {
            System.out.println("栈空，无法遍历~~");
            return;
        }
        //从栈顶开始
        for (int i = top; i >= 0; i--) {
            System.out.println(stack[i]);
        }
    }
}
